
/*
 *判断是否有第一页，若有则加载数据，没有则当前页为第一页
 */
window.onload=function()
{
    var page=$('#current_page').val();
    if(page>0){
        getPage(0);
        $('#current_page').val(0);
    }else{
        $('#current_page').val(0);

    }
    //页码值
    // var current_page=$('#current_page').val();
    // $('.Span').html(current_page);
    $('.Span').html(0);
    var all_page=$('#all_page').val();
    $('.Span2').html(all_page);
    var span=$('.Span2').html();
    $('#edit-tto-airtempcontrol').attr("max",span);
}
var ROOT="http://www.youyiku-yishu.cn";
var qbId=$('#qbId').val();

/*
 * 上一页
 */

$(".previous").click(function () {
    var page=$('#current_page').val();
    var allPage=$('#all_page').val();
    var Previous_Page=Number(page)-1;
    if(Previous_Page>=0){
        getPage(Previous_Page);
        $('#current_page').val(Previous_Page);
    }
    change();
})
/*
 * 下一页
 */
$(".next").click(function () {
    var page=$('#current_page').val();
    var allPage=$('#all_page').val();
    var Previous_Page=Number(page)+1;
    if(Previous_Page<=allPage){
        getPage(Previous_Page);
        $('#current_page').val(Previous_Page);
    }
    change();
})

/*
 *获取内页内容
 */
function getPage(page) {
    if (page > 0) {
        $.post(WST.U('WebApp/QqPhotoBooks/getType'), {page: page, qbId: qbId}, function (data) {
            var kind = data.kind;
            if (kind == 1) {
                $.post(WST.U('WebApp/QqPhotoBooks/getOrdinal'), {page: page, qbId: qbId}, function (json) {
                    // console.log(json);
                    var ordinal = json.ordinal;
                    // console.log(ordinal);
                    if (ordinal == 1) {
                        t1(page);
                    }
                    else if (ordinal == 2) {
                        t2(page);
                    }
                    else if (ordinal == 3) {
                        t3(page);
                    }
                    else if (ordinal == 4) {
                        t4(page);
                    }
                    else if (ordinal == 5) {
                        t5(page);
                    }
                    else if (ordinal == 6) {
                        t6(page);
                    }
                    else if (ordinal == 7) {
                        t7(page);
                    }
                    else if (ordinal == 8) {
                        t8(page);
                    }
                    else if (ordinal == 9) {
                        t9(page);
                    }
                    else if (ordinal == 10) {
                        t10(page);
                    }
                    else if (ordinal == 11) {
                        t11(page);
                    }
                    else if (ordinal == 12) {
                        t12(page);
                    }
                    else if (ordinal == 13) {
                        t13(page);
                    }
                    else if (ordinal == 14) {
                        t14(page);
                    }
                })
            } else if (kind == 2) {
                $.post(WST.U('WebApp/QqPhotoBooks/getInsert'), {page: page, qbId: qbId}, function (json) {
                    // console.log(json);
                    $('.Conter').html('');
                    var page = "";
                    // page+='<div class="conter1">';
                    page += '<img class="insert" src="' + WST.ROOT + '/' + json.img + '">';
                    // page+='</div>';
                    $('.Conter').html(page);
                })

            }
            else if (kind == 3) {
                t15(page);
            } else if (kind == 4) {
                t16(page);
            }

        })
    } else {

        $.post(WST.U('WebApp/QqPhotoBooks/photoBookCover'), {page: page, qbId: qbId}, function (data) {
            if(data.coverImg.indexOf("photo_cover_1") > 0){
                i=1;
            }else if(data.coverImg.indexOf("photo_cover_3") > 0){
                i=3;
            }
            console.log(i);
            if (i == 1) {

                $('.Conter').html('');
                var page = "";
                page += '<div class="cover">';
                page += '<img src="' + WST.ROOT + '/'+data.coverImg+'"id=""/>';
                if(data.personalCover){
                    page += '<div class="imgbox">';
                    page += '<img src="' + WST.ROOT + '/'+data.personalCover+'"/>';
                    page += '</div>';
                }

                page += '<div class="cover_title">'+data.bookName+'</div>';
                page += '<div class="cover_name">'+data.title+'</div>';
                page += '</div>';
                $('.Conter').html(page);

            } else if (i == 3) {
                $('.Conter').html('');
                var page = "";
                page += '<div class="cover">';
                page += '<img src="' + WST.ROOT + '/'+data.coverImg+'"id=""/>';
                if(data.personalCover){
                    page += '<div class="imgbox1">';
                    page += '<img src="' + WST.ROOT + '/'+data.personalCover+'"/>';
                    page += '</div>';
                }
                page += '<div class="cover_title1">'+data.bookName+'</div>';
                page += '<div class="cover_name1">'+data.title+'</div>';
                page += '</div>';
                $('.Conter').html(page);
            }


        })
    }
}
//页码
$('.previous').click(function(){
    var current_page=$('#current_page').val();
    var all_page=$('#all_page').val();
    $('.Span').html(current_page);
    $('.Span2').html(all_page);
})
$('.next').click(function(){
    var current_page=$('#current_page').val();
    var all_page=$('#all_page').val();
    $('.Span').html(current_page);
    $('.Span2').html(all_page);
})
/*
 *在指定字符串插入
 */
function insert_flg(str,flg,sn){
    //要插入的字符

    var newstr="";
    for(var i=0;i<str.length;i+=sn){
        var tmp=str.substring(i, i+sn);
        newstr+=tmp+flg;
        flg='';
    }
    return newstr;
}


/*
 *视频转二维码
 */
function vedioToCode(url,obj){
    var newurl='http://www.youyiku-yishu.cn/'+url;
    console.log(newurl);
    // var newurl='http://cheshi.youyiku-yishu.cn/'+url;
    // var newurl='localhost/youyiku/WebApp/QqPhotoBooks/vedioPlay/'+url;
    createErweima(newurl,obj);
    //需要qrjs2.min.js
    function createErweima(newurl,obj){
        //console.log(url,obj);
        var container1 = $(obj);
        var svgElement = document.createElement("div"),

            u = newurl,
            s = QRCode.generateSVG(u, {
                ecclevel: "M",
                fillcolor: "#FFFFFF",
                textcolor: "#373737",
                //						margin: 4,
                //						modulesize: 4
            });
        svgElement.appendChild(s);
        container1.append(svgElement);
    }
}

function toBecomeCode(){
    $(".vedioToCode").each(function(){
        var url = $(this).data("url");
        vedioToCode(url,this);
    })
}
/*
 *语音转二维码
 */
function erweima(url,obj){
    var newurl='http://www.youyiku-yishu.cn/'+url;
    // var newurl='http://cheshi.youyiku-yishu.cn/'+url;
    createErweima(newurl,obj);
    //需要qrjs2.min.js
    function createErweima(newurl,obj){
        //console.log(url,obj);
        var container1 = $(obj);
        var svgElement = document.createElement("div"),

            u = newurl,
            s = QRCode.generateSVG(u, {
                ecclevel: "M",
                fillcolor: "#FFFFFF",
                textcolor: "#373737",
                //						margin: 4,
                //						modulesize: 4
            });
        svgElement.appendChild(s);
        container1.append(svgElement);
        $('.text1').addClass('text1-wh');
        $('.text2').addClass('text2-wh');
        $('.text3').addClass('text3-wh');
        $('.text4').addClass('text4-wh');
        $('.text5').addClass('text5-wh');
        $('.text6').addClass('text6-wh');
        $('.text7').addClass('text7-wh');
        sounds();
    }
}
function sounds(){
    var page=$('.erWeiMa').html();
//	       console.log(page);
    page+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photoNewBook/yuyintiao.png" class="sounds"/>';
    $('.erWeiMa').html(page);
}
/*
 *网址变二维码
 */
function tobecode(){
    $(".erWeiMa").each(function(){
        var url = $(this).data("url");
        erweima(url,this);
    })
}

//进度条
$.fn.RangeSlider = function(cfg){
    this.sliderCfg = {
        min: cfg && !isNaN(parseFloat(cfg.min)) ? Number(cfg.min) : null,
        max: cfg && !isNaN(parseFloat(cfg.max)) ? Number(cfg.max) : null,
        step: cfg && Number(cfg.step) ? cfg.step : 1,
        callback: cfg && cfg.callback ? cfg.callback : null
    };

    var $input = $(this);
    var min = this.sliderCfg.min;
    var max = this.sliderCfg.max;
    var step = this.sliderCfg.step;
    var callback = this.sliderCfg.callback;

    $input.attr('min', min)
        .attr('max', max)
        .attr('step', step);
    $input.on("input change", function(e){
        $input.attr('value', $input.val());
        $input.css( 'background', 'linear-gradient(to right, #63E0CE, white ' + $input.val() * 100 / max + '%, white)' );
        var slider=$('.slider').val();
        $('.Span').html(slider);
        $('#current_page').val(slider);
        getPage(slider);
    });
};
var min=$("#current_page").val();
var max=$("#all_page").val();
$("#edit-tto-airtempcontrol").RangeSlider({ min: min,   max: max,  step: 1,  callback: function(target){

    }});

var change = function($input) {
    var page=$('#current_page').val();
    $('.slider').val(page);
    $('.slider').css( 'background', 'linear-gradient(to right, #63E0CE, white ' + page * 100 / max + '%, white)' );
}


function buy(photoBookId){
    var master=$('#master').val();
    master=Number(master);
    // alert(master);
    if(master>0){
        location.href = WST.U("WebApp/Orders/buylist",'photoBookId='+photoBookId);
    }else{
        wstMsg('对不起，您没有购买权限');
    }

}
/*
 *提示信息
 */
function wstMsg(msg, focusTarget, time){
    $('#wst-modal-content').html(msg);
    $('#wst-msg-modal').show();
    $('#wst-msg-modal').addClass('am-animation-fade');
    var t = time ? time : 2;
    setTimeout(function(){
        if(focusTarget){
            $('#'+focusTarget).focus();
        }
        $('#wst-msg-modal').fadeOut(1000);
    },t*1000);
}